package com.anorak.betsy.util;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.anorak.betsy.entity.Music;
import com.anorak.betsy.entity.Song;
import com.anorak.betsy.entity.Type;

/**
 * @author anorak
 */
public class ConvertUtil {

    private static final String QQ_MARK = "https://y.qq.com/n/ryqq/playlist/";

    private static final String QQ_BASE_URL = "https://c.y.qq.com/v8/fcg-bin/fcg_v8_playlist_cp.fcg?format=json&id=";


    public static Music convert(String str) {
        Music music = new Music();
        if (str.contains(QQ_MARK)) {
            music.setType(Type.QQ_MUSIC);
            str = str.replace(QQ_MARK, "");
            music.setListId(Integer.parseInt(str));
            JSONObject json = new JSONObject(HttpUtil.get(QQ_BASE_URL + music.getListId()));
            JSONArray array = new JSONArray(json.getByPath("data.cdlist.songlist[0]"));
            for (Object object : array) {
                JSONObject o = new JSONObject(object.toString());
                Song s = new Song();
                s.setName(o.get("songname").toString());
                JSONArray j = o.getJSONArray("singer");
                for (Object o1 : j) {
                    JSONObject o2 = new JSONObject(o1.toString());
                    s.getSingerName().add(o2.get("name").toString());
                }
                music.getSongList().add(s);
            }
        } else {
            music.setType(Type.UNKNOWN_MUSIC);
        }
        return music;
    }
}
